package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.ArticleDto;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.entity.YddUserData;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import com.itheima.ydd.service.IYddUserDataService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_article")
public class YddArticleController {


    @Autowired
    private IYddArticleService articleService;

    @Autowired
    private IYddCateService cateService;

    @Autowired
    private IYddUserDataService userDataService;

    /**
     * 展示所有标签列表
     *
     * @param startTime
     * @param endTime
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(@RequestParam(value = "start_time", required = false) Long startTime,
                           @RequestParam(value = "end_time", required = false) Long endTime,
                           String title, Integer cateid, Long page, Long limit) {

        Page<YddArticle> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(startTime != null, YddArticle::getCreateDate, startTime)
                .le(endTime != null, YddArticle::getCreateDate, endTime);
        wrapper.like(StringUtils.isNotEmpty(title), YddArticle::getTitle, title);
        wrapper.eq(cateid != null, YddArticle::getCateid, cateid);
        articleService.page(pageInfo, wrapper);
        List<YddArticle> records = pageInfo.getRecords();

        Page<ArticleDto> articleDtoPage = new Page<>();

        BeanUtils.copyProperties(pageInfo, articleDtoPage, "records");
        List<ArticleDto> yddArticleDtos = records.stream().map(item -> {
            ArticleDto yddArticleDto = new ArticleDto();
            BeanUtils.copyProperties(item, yddArticleDto);
            LambdaQueryWrapper<YddCate> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(YddCate::getId, item.getCateid());
            YddCate yddCate = cateService.getOne(wrapper1);
            if (yddCate != null) {
                yddArticleDto.setCatename(yddCate.getCatename());
            }
            return yddArticleDto;
        }).collect(Collectors.toList());
        articleDtoPage.setRecords(yddArticleDtos);

        return ResultData.ok("", PageResult.init(articleDtoPage.getTotal(),limit,page,articleDtoPage.getRecords()));

    }

    /**
     * 修改时数据回显
     *
     * @param yddArticle
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(YddArticle yddArticle) {

        YddArticle article = articleService.getById(yddArticle.getId());
        return ResultData.ok("",article);

    }

    /**
     * 修改文章
     *
     * @param yddArticle
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddArticle yddArticle) {

        yddArticle.setUpdateDate(System.currentTimeMillis());
        articleService.updateById(yddArticle);
        return ResultData.ok("","");

    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delte(String id) {

        YddArticle article = articleService.getById(id);
        if ("1".equals(article.getDefaultData())) {
            throw new BusinessException("系统默认数据无法删除!");
        }
        articleService.removeById(id);

        LambdaQueryWrapper<YddUserData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUserData::getArticleId, id);
        userDataService.remove(wrapper);

        return ResultData.ok("","");
    }

    /**
     * 新增文章
     *
     * @param yddArticle
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody YddArticle yddArticle) {

        yddArticle.setUpdateDate(yddArticle.getCreateDate());
        articleService.save(yddArticle);
        return ResultData.ok("","");
    }
}